package GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;

import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JCheckBox;
import javax.swing.border.LineBorder;
import javax.swing.JButton;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Calendar;

import logic.Loan;
import logic.Message;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ButtonGroup;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import javax.swing.UIManager;

public class Return_Book extends JPanel{
	
	private Client_GUI parent;
	private JLabel lblNewLabel_1;
	private JTextField textFieldBookId;
	private JTextField textFieldCopyId;
	private JTextField textFieldMemberId;
	private JLabel LBLMemberId;
	private JLabel LBLCopyId;
	private JLabel LBLBookId;
	private JLabel lblFirstInsertMember;
	private JButton btnReturnTheBook;
	private JButton btnNewButton;
	private JRadioButton rdbtnLostBook;
	private JRadioButton rdbtnValidBook;
	private JRadioButton rdbtnDamagedBook;
	private Return_Book self;
	private int BookStatus;
	
	/**
	 * @wbp.nonvisual location=372,189
	 */
	private final ButtonGroup buttonGroup = new ButtonGroup();

	
	public Return_Book(final Client_GUI parent) {
		this.self = this;
		setBackground(UIManager.getColor("inactiveCaption"));
		this.parent = parent;
		this.setSize(Client_GUI.MAIN_WIDTH, Client_GUI.MAIN_HEIGHT);
		setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Return Book");
		lblNewLabel.setForeground(Color.BLUE);
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 21));
		lblNewLabel.setBounds(333, 37, 131, 40);
		add(lblNewLabel);
		
		JLabel lblBookId = new JLabel("Book ID:");
		lblBookId.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblBookId.setBounds(85, 151, 77, 14);
		add(lblBookId);
		
		JLabel lblCopyId = new JLabel("Copy ID:");
		lblCopyId.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblCopyId.setBounds(85, 203, 77, 14);
		add(lblCopyId);
		
		
		
		lblFirstInsertMember = new JLabel("first insert member id !!!!!");
		lblFirstInsertMember.setForeground(Color.RED);
		lblFirstInsertMember.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblFirstInsertMember.setBounds(146, 360, 161, 14);
		add(lblFirstInsertMember);
		
		
		
		textFieldBookId = new JTextField();
		textFieldBookId.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				lblFirstInsertMember.setVisible(false);
				lblNewLabel_1.setVisible(false);
				parent.safeToClear = false ;
			}
		});
		textFieldBookId.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldBookId.setBounds(162, 142, 138, 27);
		add(textFieldBookId);
		textFieldBookId.setColumns(10);
		
		textFieldCopyId = new JTextField();
		textFieldCopyId.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				lblFirstInsertMember.setVisible(false);
				lblNewLabel_1.setVisible(false);
				parent.safeToClear =false ;
			}
		});
		textFieldCopyId.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldCopyId.setColumns(10);
		textFieldCopyId.setBounds(162, 192, 138, 29);
		add(textFieldCopyId);
		
		LBLBookId = new JLabel("");
		LBLBookId.setBounds(312, 142, 28, 27);
		add(LBLBookId);
		
		LBLCopyId = new JLabel("");
		LBLCopyId.setBounds(312, 192, 28, 29);
		add(LBLCopyId);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0), 3));
		panel.setBounds(498, 136, 208, 117);
		add(panel);
		panel.setLayout(null);
		
		JLabel lblRepotsOnBook = new JLabel("Book Condition");
		lblRepotsOnBook.setBounds(21, 12, 189, 14);
		panel.add(lblRepotsOnBook);
		lblRepotsOnBook.setForeground(Color.RED);
		lblRepotsOnBook.setFont(new Font("Tahoma", Font.BOLD, 13));
		
		rdbtnDamagedBook = new JRadioButton("Damaged Book");
		rdbtnDamagedBook.setBounds(32, 60, 109, 23);
		buttonGroup.add(rdbtnDamagedBook);
		rdbtnDamagedBook.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				BookStatus = Loan.LOAN_BOOK_DAMAGED;
				ValidateData(false);
			}
		});
		panel.add(rdbtnDamagedBook);
		
		rdbtnValidBook = new JRadioButton("Valid Book");
		rdbtnValidBook.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				BookStatus = Loan.LOAN_BOOK_VALID;
				ValidateData(false);
			}
		});
		rdbtnValidBook.setBounds(32, 38, 109, 23);
		buttonGroup.add(rdbtnValidBook);
		panel.add(rdbtnValidBook);
		
		rdbtnLostBook = new JRadioButton("Lost Book");
		rdbtnLostBook.setBounds(32, 82, 109, 23);
		buttonGroup.add(rdbtnLostBook);
		rdbtnLostBook.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				BookStatus = Loan.LOAN_BOOK_LOST;
				ValidateData(false);
			}
		});
		panel.add(rdbtnLostBook);
		
		JLabel lblMemberId = new JLabel("Member ID:");
		lblMemberId.setFont(new Font("Tahoma", Font.BOLD, 12));
		lblMemberId.setBounds(87, 310, 77, 14);
		add(lblMemberId);
		
		textFieldMemberId = new JTextField();
		textFieldMemberId.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				lblFirstInsertMember.setVisible(false);
				lblNewLabel_1.setVisible(false);
				parent.safeToClear = false ;
			}
		});
		textFieldMemberId.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				ValidateData(false);
			}
		});
		textFieldMemberId.setColumns(10);
		textFieldMemberId.setBounds(164, 299, 138, 29);
		add(textFieldMemberId);
		
		LBLMemberId = new JLabel("");
		LBLMemberId.setBounds(312, 299, 28, 29);
		add(LBLMemberId);
		
		btnReturnTheBook = new JButton("Return the Book ");
		btnReturnTheBook.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnReturnTheBook.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				ValidateData(true);
			}
		});
		btnReturnTheBook.setEnabled(false);
		btnReturnTheBook.setBounds(162, 428, 161, 56);
		add(btnReturnTheBook);
		
		JButton btnCancel = new JButton("Cancel ");
		btnCancel.setFont(new Font("Tahoma", Font.BOLD, 13));
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Home home = new Home(parent,parent.client.publicMessages);
				VerifyDialog verify = new VerifyDialog(parent,home,false);
			}
		});
		btnCancel.setBounds(522, 428, 161, 56);
		add(btnCancel);
		
		lblNewLabel_1 = new JLabel("You must insert Book id and Copy id!!!");
		lblNewLabel_1.setForeground(Color.RED);
		lblNewLabel_1.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewLabel_1.setBounds(263, 393, 281, 40);
		add(lblNewLabel_1);
		
		JButton btnSearceMember = new JButton("Search member");
		btnSearceMember.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SearchMember topaint = new SearchMember(parent, SearchMember.SEARCH_FOR_RETURN_LOAN, self);
				parent.currentMain = topaint;
				parent.RepaintMain(topaint);
				
			}
		});
		btnSearceMember.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnSearceMember.setBounds(354, 299, 161, 29);
		add(btnSearceMember);
		
		JLabel lblByBook = new JLabel("By Book");
		lblByBook.setForeground(Color.RED);
		lblByBook.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblByBook.setBounds(85, 93, 131, 29);
		add(lblByBook);
		
		btnNewButton = new JButton("Get Info");
		btnNewButton.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				ValidateData(true);
			}
		});

		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				parent.safeToClear = true;
				SearchLoan topaint = new SearchLoan(parent, SearchLoan.SEARCH_FOR_RETURN, self);
				parent.currentMain = topaint;
				topaint.SetLoad(textFieldMemberId.getText());
			}
		});
		btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 11));
		btnNewButton.setForeground(Color.DARK_GRAY);
		btnNewButton.setBounds(184, 330, 89, 23);
		btnNewButton.setEnabled(false);
		add(btnNewButton);
		
		JLabel lblByMember = new JLabel("By Member");
		lblByMember.setForeground(Color.RED);
		lblByMember.setFont(new Font("Tahoma", Font.BOLD, 13));
		lblByMember.setBounds(85, 259, 131, 29);
		add(lblByMember);
		
		
		lblNewLabel_1.setVisible(false);
		lblFirstInsertMember.setVisible(false);
		
	}
	
	/**
	 * This function sets the user ID that was searched for
	 * (Called automatically)
	 * @param uid - the UID to set
	 */
	public void SetUID(String uid){
		textFieldMemberId.setText(uid);
		btnNewButton.setEnabled(true);
	}
	
	
	/**
	 * This function sets the user ID that was searched for
	 * (Called automatically)
	 * @param bid - the BID to set
	 * @param bcid - the BCID to set
	 */
	public void SetLoan(int bid,int bcid){
		textFieldBookId.setText(String.valueOf(bid));
		textFieldCopyId.setText(String.valueOf(bcid));
	}
	
	
	private void ValidateData(boolean fromOK){

		ImageIcon okImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/ok.png"));
		ImageIcon OK = new ImageIcon (okImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		
		ImageIcon badImageIcon = new ImageIcon(getClass().getClassLoader().getResource("resource/bad.png"));
		ImageIcon BAD = new ImageIcon (badImageIcon.getImage().getScaledInstance(24, 24, java.awt.Image.SCALE_SMOOTH));
		String temp; // the string to be tested on each test
		boolean valid = true;
		
		//Validate BID
		temp = textFieldBookId.getText();
		if (!temp.equals(""))
		{
		try{
			char c = temp.charAt(0);
			if (c == '+' || c == '-') {
				LBLBookId.setIcon(BAD);
				valid = false;
			}
			else LBLBookId.setIcon(OK);	
			Integer.parseInt(temp);	
		}catch(NumberFormatException e){
			LBLBookId.setIcon(BAD);
			valid = false;
		}
		}
		else LBLBookId.setIcon(null);
		
		//Validate UId
		temp = textFieldMemberId.getText();
		if (!temp.equals(""))
		{
		try{
			char c = temp.charAt(0);
			if (c == '+' || c == '-' || temp.length()!=9) {
				LBLMemberId.setIcon(BAD);
				valid = false;
			}
			else LBLMemberId.setIcon(OK);	
			Integer.parseInt(temp);	
		}catch(NumberFormatException e){
			LBLMemberId.setIcon(BAD);
			valid = false;
		}
		}
		else LBLMemberId.setIcon(null);
		
		/**
		 * Validate copy ID
		 */
		temp = textFieldCopyId.getText();
		if (!temp.equals(""))
		{
		try{
			char c = temp.charAt(0);
			if (c == '+' || c == '-') {
				LBLCopyId.setIcon(BAD);
				valid = false;
			}
			else LBLCopyId.setIcon(OK);	
			Integer.parseInt(temp);	
		}catch(NumberFormatException e){
			LBLCopyId.setIcon(BAD);
			valid = false;
		}
		}
		else LBLCopyId.setIcon(null);
		
		
		//Checks whether to enable return button or not
		btnReturnTheBook.setEnabled(false);
		if ((!textFieldBookId.getText().equals("")) && (!textFieldCopyId.getText().equals("")))
			if ((rdbtnValidBook.isSelected() == true) || (rdbtnDamagedBook.isSelected() == true) || (rdbtnLostBook.isSelected() == true)) btnReturnTheBook.setEnabled(true);

		//Checks whether to enable GetInfo button
		if (textFieldMemberId.getText().equals("")) btnNewButton.setEnabled(false);
		else btnNewButton.setEnabled(true);
		
		
		if (fromOK)
		{
			
			if(textFieldBookId.getText().equals("") || textFieldCopyId.getText().equals("") )
			{
		    valid = false;
			}
		
		}
		
		
		if (valid && fromOK){
			int action = Message.RETUREN_BOOK_REQUEST;
			ArrayList<String> index = new ArrayList<String>();
			ArrayList<Object> data = new ArrayList<Object>();
						
			index.add("BID");
			data.add(textFieldBookId.getText());
		
			index.add("BCID");
			data.add(textFieldCopyId.getText());
		
			index.add("BOOKSTATUS");
			data.add(BookStatus);
			
			index.add("UID");
			data.add(textFieldMemberId.getText());
					
			
			Message toSend = new Message(index, data, action);
			
			if (parent.sendMessageToServer(toSend) == 1){//message sent successfully
				parent.SetLoading(true);
								
			}else{//message send failed
				//TODO verify extreme situation
				}
		
	}
	
}
}
